//
//  SHMenuController.m
//  吃货·上海
//
//  Created by 濮亚军 on 15/11/24.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "SHMenuController.h"
#import "SHMenuCell.h"
#import "DetailController.h"
#import "MBProgressHUD.h"
#import "SVPullToRefresh.h"

@interface SHMenuController () <UITableViewDataSource ,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tabelView;

@end

static NSString *cellIndextifier = @"cell";
@implementation SHMenuController

- (NSMutableArray *)modleArray
{
    if (_modelArray == nil) {
        self.modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [self getDataFromMyDB];
    
    self.tabelView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    self.tabelView.dataSource = self;
    self.tabelView.delegate  =self;
    
    self.tabelView.separatorStyle = NO;
    [self.tabelView registerClass:[SHMenuCell class] forCellReuseIdentifier:cellIndextifier];
    
    NSLog(@"%ld",self.modleArray.count);
    
    self.navigationController.navigationBar.translucent =NO;

    [self.view addSubview:_tabelView];

    
    __weak SHMenuController *blockSelf = self;
    [self.tabelView addPullToRefreshWithActionHandler:^{
        //下拉加载
        [blockSelf getDataFromMyDB];
    }];
    
    [self.tabelView addInfiniteScrollingWithActionHandler:^{
       
        //上拉加载更多
        
    }];
}


#pragma mark -  下拉
- (void)updataForRow
{
    [self getDataFromMyDB];
}

#pragma mark -  上拉
- (void)insertRowAtBottom
{
    //
    NSMutableArray *array = self.modelArray;
    [self getDataFromMyDB];
    if (_modelArray.count == array.count) {
        return;
    }else
    {
        [self.tabelView beginUpdates];
        [self.tabelView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.modelArray.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
        [self.tabelView endUpdates];
    }
    
    [self.tabelView.infiniteScrollingView stopAnimating];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modleArray.count;
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return KScreenHeight / 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHMenuCell *cell  =[tableView dequeueReusableCellWithIdentifier:cellIndextifier forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    SHMenuModel *model = self.modleArray[indexPath.row];
//
    cell.model = model;
    
    cell.backgroundColor = KMyColor ;
    return cell;
}

#pragma mark - 点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailController *detailVc = [[DetailController alloc]init];
    detailVc.model = self.modleArray[indexPath.row];
    [self.navigationController pushViewController:detailVc animated:NO];
}

#pragma mark - 数据请求
- (void)getDataFromMyDB
{
    BmobQuery *bqeury = [BmobQuery queryWithClassName:@"Menu"];
    [bqeury findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        for (BmobObject *obj in array) {
            SHMenuModel *model =[[SHMenuModel alloc]init];
            
            model.title = [obj objectForKey:@"Dishname"];
            BmobFile *file = [obj objectForKey:@"icon"];
            model.imageUrl = file.url;
            model.info = [obj objectForKey:@"ifno"];
            model.myClass = [obj objectForKey:@"class"];
            model.mainmatcrials = [obj objectForKey:@"mainmatcrials"];
            model.makingMethod = [obj objectForKey:@"method"];
            model.makingmaterial = [obj objectForKey:@"material"];
            
            [self.modelArray addObject:model];
            
        }
        
        [self.tabelView.pullToRefreshView stopAnimating];

        [MBProgressHUD hideHUDForView:self.view animated:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tabelView reloadData];
        });
    }];
}


@end
